package com.bedpotato.thread;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.bedpotato.constants.ProxyClawerConstants;
import com.bedpotato.http.BasicHttpMethod;
import com.bedpotato.po.ProxyPO;
import com.bedpotato.service.ProxyClawerService;

@Component
public class GetproxySiteClawerThread implements Runnable {
	Logger logger = Logger.getLogger(ProxyClawerService.class);
	@Resource
	ProxyClawerService proxyClawerService;
	public static void main(String[] args) {
		GetproxySiteClawerThread getproxySiteClawerThread = ProxyClawerConstants.applicationContext.getBean("getproxySiteClawerThread",GetproxySiteClawerThread.class);
		getproxySiteClawerThread.run();
	}
	@Override
	public void run() {
		logger.info("Getproxy信息抓取启动");
		Set<ProxyPO> proxySet = getGetProxyData();
		proxyClawerService.saveProxy(proxySet);
		logger.info("Getproxy信息抓取结束");
	}

	public Set<ProxyPO> getGetProxyData() {
		BasicHttpMethod httpMethod = new BasicHttpMethod();
		Set<ProxyPO> proxySet = new HashSet<ProxyPO>();
		for (int i = 1; i <= 5; i++) {
			try {
				String pageStr = httpMethod
						.get("http://www.getproxy.jp/china/"+i);
				String[] contentStrArray = StringUtils.substringsBetween(
						pageStr, "<strong>", "</strong>");
				for (String contentStr : contentStrArray) {
					if (!contentStr.isEmpty()
							&& StringUtils
									.isNumeric(contentStr.substring(0, 1))) {
						ProxyPO proxyPO = new ProxyPO();
						proxyPO.setIp(contentStr.substring(0,
								contentStr.indexOf(":")));
						proxyPO.setPort(contentStr.substring(
								contentStr.indexOf(":") + 1,
								contentStr.length()));
						// System.out.println(proxyPO.getIp() + ":"
						// + proxyPO.getPort());
						proxySet.add(proxyPO);
					}
				}
			} catch (Exception e) {
				logger.error("Getproxy " + e);
			}
		}
		return proxySet;
	}
}
